﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ELS.User;
using System.Threading;
using ELS.BackupRestore;
using System.IO;
using System.Net;
using ELS.ReportStatistic;

namespace ELS
{
    public partial class MainForm : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm" /> class.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();                       
        }
        /// <summary>
        /// The user controller
        /// </summary>
        UserController userController = new UserController();
        /// <summary>
        /// The current username
        /// </summary>
        string currentUsername = String.Empty;
        private bool isFisrtLoadTabPageDocument = true;
        private bool isFisrtLoadTabPageReportStatistic = true;
        private bool isFisrtLoadTabPageSystem = true;
        private bool isFisrtLoadTabPageTemplateDocument = true;
        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm" /> class.
        /// </summary>
        /// <param name="username">The username.</param>
        public MainForm(string username)
        {
            InitializeComponent();            
            currentUsername = username;
            lblUsername.Text = userController.GetFullnameByUsername(username);            
            List<int> groupIds = userController.CheckGroupByUsername(username);

            if (groupIds.Count == 1 && groupIds[0] == UserCommon.UNKNOWN)
            {
                foreach (Control crtl in tabPageTemplateDocument.Controls)
                {
                    crtl.Enabled = false;
                }
                foreach (Control crtl in tabPageSystem.Controls)
                {
                    crtl.Enabled = false;
                }
                foreach (Control crtl in tabPageDocument.Controls)
                {
                    crtl.Enabled = false;
                }
                foreach (Control crtl in tabPageReportStatistic.Controls)
                {
                    crtl.Enabled = false;
                }                
            }
            else if (groupIds.Contains(UserCommon.ADMIN))
            {
                // Full role right
            }
            else
            {
                tabControlMainFrm.Controls.Remove(tabPageTemplateDocument);
                tabControlMainFrm.Controls.Remove(tabPageSystem);
                tabControlMainFrm.Controls.Remove(tabPageDocument);
                tabControlMainFrm.Controls.Remove(tabPageReportStatistic);
                if (groupIds.Contains(UserCommon.REPORTER))
                {
                    tabControlMainFrm.Controls.Add(tabPageReportStatistic);
                }
                if (groupIds.Contains(UserCommon.IMPORTER))
                {
                    tabControlMainFrm.Controls.Add(tabPageTemplateDocument);
                    tabControlMainFrm.Controls.Add(tabPageDocument);
                }             
            }

            // Binding data to default tab
            tabControlMainFrm.SelectedTab = tabPageUserInfo;
            foreach (Control ctl in pnlUserInfo.Controls)
            {
                ctl.Dispose();
            }
            AddEditUserForm userFrm = new AddEditUserForm(currentUsername, txtStatus, pbStatus, pnlUserInfo, UserCommon.VIEW_INFO_USER);
            userFrm.TopLevel = false;
            userFrm.Parent = pnlUserInfo;
            userFrm.Visible = true;
        }
        private void linkFullname_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DialogResult dialogConfirm = MessageBox.Show(UserCommon.LogOut_Message, UserCommon.LogOut_MessageBox_Title, MessageBoxButtons.YesNo);
            if (dialogConfirm == DialogResult.Yes)
            {
                this.Dispose();
                new LogInForm().Show();
            }
        }

        private void close_app(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControlMainFrm.SelectedTab == tabControlMainFrm.TabPages["tabPageTemplateDocument"])
            {
                if (isFisrtLoadTabPageTemplateDocument)
                {
                    foreach (Control ctl in pnlTemplateForm.Controls)
                    {
                        ctl.Dispose();
                    }

                    ELS.Template.ListTemplateForm listUserFrm = new ELS.Template.ListTemplateForm(templateTxtStatus, templatePbStatus, pnlTemplateForm);
                    listUserFrm.TopLevel = false;
                    listUserFrm.Parent = pnlTemplateForm;
                    listUserFrm.Visible = true;

                    isFisrtLoadTabPageTemplateDocument = false;
                }
            }
            else if (tabControlMainFrm.SelectedTab == tabControlMainFrm.TabPages["tabPageDocument"])
            {                
                if (isFisrtLoadTabPageDocument) {
                    // To Do ...
                    isFisrtLoadTabPageDocument = false;
                }
            }
            else if (tabControlMainFrm.SelectedTab == tabControlMainFrm.TabPages["tabPageReportStatistic"])
            {
                
                if (isFisrtLoadTabPageReportStatistic) {

                    foreach (Control ctl in pnlMainForm.Controls)
                    {
                        ctl.Dispose();
                    }
                    ELS.ReportStatistic.ReportStatistic chartingFrm = new ReportStatistic.ReportStatistic(txtStatus, pbStatus, pnlReportStatistic);
                    chartingFrm.TopLevel = false;
                    chartingFrm.Parent = pnlReportStatistic;
                    chartingFrm.Visible = true;

                    isFisrtLoadTabPageReportStatistic = false;
                }
            }
            else if (tabControlMainFrm.SelectedTab == tabControlMainFrm.TabPages["tabPageSystem"])
            {
                if (isFisrtLoadTabPageSystem)
                {
                    foreach (Control ctl in pnlMainForm.Controls)
                    {
                        ctl.Dispose();
                    }
                    ListUserForm listUserFrm = new ListUserForm(txtStatus, pbStatus, pnlMainForm);
                    listUserFrm.TopLevel = false;
                    listUserFrm.Parent = pnlMainForm;
                    listUserFrm.Visible = true;

                    isFisrtLoadTabPageSystem = false;
                }
            }
            else if (tabControlMainFrm.SelectedTab == tabControlMainFrm.TabPages["tabPageUserInfo"])
            {
                foreach (Control ctl in pnlUserInfo.Controls)
                {
                    ctl.Dispose();
                }
                AddEditUserForm userFrm = new AddEditUserForm(currentUsername, txtStatus, pbStatus, pnlUserInfo, UserCommon.VIEW_INFO_USER);
                userFrm.TopLevel = false;
                userFrm.Parent = pnlUserInfo;
                userFrm.Visible = true;
            }                        
        }

        private void tabStripUserManagement_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (tabStripUserManagement.SelectedTab == tabStripBtnUser)
            {
                foreach (Control ctl in pnlMainForm.Controls)
                {
                    ctl.Dispose();
                }
                ListUserForm listUserFrm = new ListUserForm(txtStatus, pbStatus, pnlMainForm);
                listUserFrm.TopLevel = false;
                listUserFrm.Parent = pnlMainForm;
                listUserFrm.Visible = true;
            }
            else if (tabStripUserManagement.SelectedTab == tabStripBtnBackup)
            {
                foreach (Control ctl in pnlMainForm.Controls)
                {
                    ctl.Dispose();
                }
                BackUpForm bkFrm = new BackUpForm(txtStatus, pbStatus, pnlMainForm);
                bkFrm.TopLevel = false;
                bkFrm.Parent = pnlMainForm;
                bkFrm.Visible = true;
            }
            else if (tabStripUserManagement.SelectedTab == tabStripBtnRestore)
            {
                foreach (Control ctl in pnlMainForm.Controls)
                {
                    ctl.Dispose();
                }
                RestoreForm restoreFrm = new RestoreForm(txtStatus, pbStatus, pnlMainForm);
                restoreFrm.TopLevel = false;
                restoreFrm.Parent = pnlMainForm;
                restoreFrm.Visible = true;
            }
        }

        private void linkUserGuide_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Title = "Save file as...";
            dialog.Filter = "Text files (*.doc)|*.doc|All files (*.*)|*.*";
            dialog.FileName = UserCommon.fileNameUserManual;
            dialog.RestoreDirectory = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string Src = UserCommon.filePathUserManual;
                string Dest = dialog.FileName;
                if (Src == Dest) return;
                File.Delete(Dest);
                WebClient WC = new WebClient();
                WC.DownloadFileAsync(new Uri(Src), Dest);
            }
        }
        
    }
}
